﻿using DynamicControllersGeneration;

namespace ClientOverflow.ViewModels.Register
{
    /// <summary>
    /// EmailValidator is an implementation of IValidator. This validator checks if some string match the
    /// expected regular expression of email pattern. The regex validator is passed to the dynamic controllers 
    /// generation framework which calls the validator from the dynamic proxy.
    /// </summary>
    internal class EmailValidator : IValidator
    {
        private const string EMAIL_REGEX_PATTERN =
            @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";
        private const string EMAIL_ERROR_MESSAGE = "Illegal email";

        private readonly RegexValidator regexValidator;

        public EmailValidator()
        {
            regexValidator = new RegexValidator(EMAIL_REGEX_PATTERN, EMAIL_ERROR_MESSAGE);
        }

        public string Validate(object value)
        {
            return regexValidator.Validate(value);
        }
    }
}